<?php	namespace Home\Controller;
class IndentController extends CommonController
{
	protected $db;
	public function __init()
	{
		$this->db = new \Common\Model\Indent;
	}
	public function index()
	{
		if(!isset($_SESSION['yid'])){
			View::success('请先登录',U('Logo/index'));
		}
			$yid = $_SESSION['yid'];
			$Indendata = $this->db->where('user_yid',$yid)->get();
			foreach ($Indendata as $k => $v) {
				$Indendata[$k]['goods'] = Db::table('orderfrom')->where('indent_did',$v['iid'])->get();
				foreach ($Indendata[$k]['goods'] as $ke => $va) {
					$Indendata[$k]['goods'][$ke]['explain'] = explode(',', $va['explain']);
					$Indendata[$k]['goodss'][] = Db::table('goods')->where('sid',$va['shop_sid'])->get();
				}
			}
//			p($Indendata);
		View::with('Indendata',$Indendata);
		View::make();
	}
	public function add()
	{
		if(IS_AJAX){
			$data = explode(',', $_POST['bzhu']);
//			p($data);
			$did = $data[0];
			$benzu = $data[1];
			$SiteData = Db::table('site')->where('did',$did)->first();
			if(isset($_SESSION['shop'])){
				$Data = array(
				'order'=>date("YmdHis",time()).$did,
				'consignee'=>$SiteData['consignee'],
				'shipping'=>$SiteData['province'].$SiteData['city'].$SiteData['district'].$SiteData['shipping'],
				'price'=>intval($_SESSION['shop']['total']),
				'generated'=>time(),
				'user_yid'=>$SiteData['user_did'],
				'ostatus'=>'未付款',
				'remark'=>$benzu
				);
			}else{
				$Data = array(
				'order'=>date("YmdHis",time()).$did,
				'consignee'=>$SiteData['consignee'],
				'shipping'=>$SiteData['province'].$SiteData['city'].$SiteData['district'].$SiteData['shipping'],
				'price'=>$_SESSION['cart']['total'],
				'generated'=>time(),
				'user_yid'=>$SiteData['user_did'],
				'ostatus'=>'未付款',
				'remark'=>$benzu
				);
			}
			$this->db->store($Data);
			unset($_SESSION['shop']);
			unset($_SESSION['cart']);
			echo 1;
		}
	}
}
